#!/bin/bash
#usage: bash check_bras_connect.sh (offcamps)

if [ -z "$(id | grep "uid=0")" ];then
	echo "You're not the root!"
	exit 1
fi

INTERFACE=ppp$(cat ../config/extra_options | grep unit | grep -o [0-9])
DEVICE=$(route -n | grep -e eth -e wlan )
OFF_CAMPUS_BRAS_IP=$(sed -n "2p" ../config/bras_ip)

if [ ! -e /tmp/njubras-for-linux.lock ];then
	touch /tmp/njubras-for-linux.lock
	chmod 600 /tmp/njubras-for-linux.lock
fi

if [ -z "$(route -n | grep 172.16.0.0)" ] && [ "$1" != "offcampus" ];then
	if [ -n "$DEVICE" ];then
		bash ./routes_change.sh add 
	fi
elif [ -z "$(route -n | grep $OFF_CAMPUS_BRAS_IP)" ] && [ "$1" = "offcampus" ];then
	if [ -n "$DEVICE" ];then
		bash ./routes_change.sh add offcampus
	fi
fi

if [ -z "$(route -n | grep $INTERFACE)" ];then 
	#pkill xl2tpd
	#xl2tpd -c /etc/xl2tpd/njubras.conf
	echo "d njubras" >>/var/run/xl2tpd/l2tp-control
	sleep 0.5
	echo "c njubras" >>/var/run/xl2tpd/l2tp-control
	sleep 6.0
	if [ -n "$(route -n | grep $INTERFACE)" ];then
		bash ./routes_change.sh add $1
	fi

fi

exit 0
